package com.ruoyi.web.core.config;

import com.ruoyi.web.controller.wexin.PictureController;
import com.ruoyi.weixin.entity.Picture;
import com.ruoyi.weixin.service.*;
import com.ruoyi.weixin.untils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

@Component
public class FileDeleteScheduler {

    @Autowired
    PictureService pictureService;
    @Autowired
    UserService userService;
    @Resource
    PicUrlService picUrlService;

    @Autowired
    MinioService minioService;

    @Scheduled(cron = "0 0 0 L * * ")
//    每个月最后一天执行零点执行
    public void deleteFile() {
        pictureService.findLajiAndDelete();
    }


    @Scheduled(cron = "0 0 0 L * * ")
    public void deleteFileAndfei() {
//        List<String> users = userService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
//        List<String> frents = picUrlService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
//        List<String> prcurls = pictureService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
//        prcurls.addAll(frents);
//        prcurls.addAll(users);
//        List<String> ossUrls = minioService.listObjectNames("xingyue");
//
//
//        System.err.println(ossUrls);
//        System.err.println(prcurls.size()+"-----------"+ossUrls.size());
//
//        List<String> finallyList = new ArrayList<>();
//        for (String ossUrl : ossUrls) {
//            if (!prcurls.contains(ossUrl)) {
//                finallyList.add(ossUrl);
//                Pattern pattern = Pattern.compile("xingyue/(.*)");
//                Matcher matcher = pattern.matcher(ossUrl);
//                if (matcher.find()) {
//                    String desiredPart = matcher.group(1);
//                    System.err.println(desiredPart);
//                    minioService.removeObject("xingyue", desiredPart);
//                }
//            }
//        }
//        System.err.println(finallyList);
        List<String> ossUrls = OssUtil.getObjList();
        List<String> users = userService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
        List<String> frents = picUrlService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
        List<String> prcurls = pictureService.list().stream().map(item -> item.getPicUrl()).collect(Collectors.toList());
        prcurls.addAll(frents);
        prcurls.addAll(users);
        ArrayList<String> hostUrls = new ArrayList<>();
        for (String pic : prcurls) {
            String result = null;
            Pattern pattern = Pattern.compile("com/(.*)");
            Matcher matcher = pattern.matcher(pic);
            if (matcher.find()) {
                result = matcher.group(1);
                if (result.startsWith("HongMeng")) {
                    hostUrls.add(result);
                }
            } else {
                System.out.println("No match found");
            }
        }
        ArrayList<String> finallyList = new ArrayList<>();
        for (String ossUrl : ossUrls) {
            if (!hostUrls.contains(ossUrl)){
                OssUtil.delete(ossUrl);
            }
        }
    }
}